package com.zhu.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.commons.utils.EmptyUtil;
import com.zhu.commons.utils.R;
import com.zhu.service.edu.pojo.EduCourse;
import com.zhu.service.edu.pojo.vo.*;
import com.zhu.service.edu.service.IEduCourseCollectService;
import com.zhu.service.edu.service.IEduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/admin/edu/course")
@Api(tags = "课程管理")
@Slf4j
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private IEduCourseService courseService;

    @ApiOperation(value = "新增课程")
    @PostMapping("save-course-info")
    public R saveCourseInfo(@RequestBody(required = true) CourseInfoVo courseInfoVo){
              String courseId= courseService.saveCourseInfo(courseInfoVo);
              if(!EmptyUtil.isEmpty(courseId)){

                  return   R.ok().data("courseId",courseId).message("保存成功！");
              }
              return R.error().message("保存失败！");
    }

    @ApiOperation(value = "根据id查询课程")
    @GetMapping("/course-info/{id}")
    public R getId(@PathVariable("id")String id){
      CourseInfoVo courseInfoVo= courseService.getCourseInfoById(id);
      if(courseInfoVo!=null){
          return R.ok().data("item",courseInfoVo);
      }
      return R.error().message("数据不存在");
    }

    @ApiOperation(value = "更新课程")
    @PutMapping("/course-info/update")
    public R updateSubjectById(@RequestBody CourseInfoVo courseInfoVo){

        if(courseService.updateCourseInfoById(courseInfoVo)){
            return R.ok().message("更新成功！");
        }
        return R.error().message("更新失败！");
    }

    @ApiOperation(value = "课程分页列表")
    @GetMapping("list/{currentPage}/{size}")
    public R listPage(@PathVariable(value = "currentPage" ,required = true)Long currentPage,
                             @PathVariable(value = "size",required = true)Long size,
                             CourseQueryVo courseQueryVo){
       PageBean pageBean= courseService.selectPages(currentPage,size,courseQueryVo);
       return R.ok().data("item",pageBean);

    }
    @ApiOperation(value = "根据id删除课程")
    @DeleteMapping("/remove/{id}")
    public R deleteCourseById(@PathVariable String id){
        if(courseService.removeCourseById(id)){
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");
    }

    @ApiOperation(value = "根据id发布课程信息")
    @GetMapping("/course-publish/{id}")
    public R getCoursePublishById(@PathVariable String id){
      CoursePublishVo coursePublishVo= courseService.getCoursePublishById(id);
      if(coursePublishVo!=null){
          return R.ok().data("item",coursePublishVo);
      }
      return R.error().message("数据不存在");
    }

    @ApiOperation(value = "根据id发布课程")
    @GetMapping("/publish-course/{id}")
    public R publishCourse(@PathVariable String id){
         Boolean result = courseService.publishCourse(id);
        if(result){
            return R.ok().message("课程发布成功！");
        }
        return R.error().message("课程发布失败！");
    }

}
